package com.hui.security.sys.controller;

import com.hui.security.common.response.ResultMapper;
import com.hui.security.common.response.ResultVO;
import com.hui.security.sys.entity.Permission;
import com.hui.security.sys.service.PermissionService;
import com.hui.security.sys.vo.PermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b><code>ResourceController</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 1:18.
 *
 * @author HuWeihui
 */
@RestController
@RequestMapping("/v1")
@Api(value = "权限模块-用户权限API", tags = "permission")
@Slf4j
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * Create permission result vo.
     *
     * @param permissionVO  the permission vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "创建权限", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/permission/{id}")
    public ResultVO createPermission(@RequestBody @Validated PermissionVO permissionVO, BindingResult bindingResult) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionVO, permission);
        return ResultMapper.ok(permissionService.add(permission));
    }

    /**
     * Delete permission result vo.
     *
     * @param permissionId the permission id
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "删除权限", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DeleteMapping("/permission/{id}")
    public ResultVO deletePermission(@PathVariable String permissionId) {
        permissionService.delete(permissionId);
        return ResultMapper.ok();
    }

    /**
     * Update permission result vo.
     *
     * @param permissionVO  the permission vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "更新权限", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PutMapping("/permission")
    public ResultVO updatePermission(@RequestBody @Validated PermissionVO permissionVO, BindingResult bindingResult) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionVO, permission);
        permissionService.update(permission);
        return ResultMapper.ok();
    }

    /**
     * Gets permission.
     *
     * @param permissionId the permission id
     * @return the permission
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询权限详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/permission/{id}")
    public ResultVO getPermission(@PathVariable("id")String permissionId) {
        Permission permission = permissionService.get(permissionId);
        return ResultMapper.ok(permission);
    }

    /**
     * List roles by user result vo.
     *
     * @param roleId the role id
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询角色相关的权限", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/role/{id}/permissions")
    public ResultVO listPermissionsByRole(@PathVariable("id") String roleId){
        List<Permission> permissionList = permissionService.listByRoleId(roleId);
        return ResultMapper.ok(permissionList);
    }


    /**
     * List permissions by user id result vo.
     *
     * @param userId the user id
     * @return the result vo
     * @author HuWeihui
     * @since hui_project v1
     */
    @ApiOperation(value = "查询用户所属的所有权限", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/user/{id}/premissions")
    public ResultVO listPermissionsByUserId(@PathVariable("id") String userId) {
        List<Permission> permissionList = permissionService.listByUserId(userId);
        return ResultMapper.ok(permissionList);
    }
}
